define(
		[ "PDUtilDir/util", "PDUtilDir/tool", "PDUtilDir/slidebar", "PDUtilDir/inputSelect", "PDUtilDir/fileupload/multiFileUpload" ],
		function(Util, Tool, Slidebar, inputSelect, MFU) {
			function addSupportInit($scope, $compile){
				return new addSupport($scope, $compile);
			};
			function addSupport($scope, $compile){
				this.$scope = $scope;
				this.$compile = $compile;
			};
			addSupport.fn = addSupport.prototype;
			addSupport.fn.init = function(){
				this.entity = {
					addRessId: ""
				};
				this.bind = {
					images: new Array()
				};
				this.cache = {
					inputCity: undefined,
					inputArea: undefined
				}
			};
			addSupport.fn.digest = function(){
				 this.$scope.$digest();
			};
			addSupport.fn.run = function() {
				var _this = this;
				_this.init();
				this.slidebars = Slidebar({
					size : 'lg',
					url : getServer()
							+ "/static/app/spots/mySpotInfos/views/add.html",
					$scope: this.$scope,
					$compile: this.$compile,
					afterLoad : function($Panel) {
						_this.initialization();
					}
				});
			};
			addSupport.fn.initialization = function(){
				this.baseInit();
			};
			
			addSupport.fn.init_city = function(pid) {
				var _this = this;
				$.ajax({
					url : getServer() + "/flea/hclv/getCity.htmls",
					data:{'pcode': pid},
					success : function(data) {
                        if (data.status != 200) {
                            return;
                        }
						if(_this.cache.inputCity){
							_this.cache.inputCity.refreshPanel(data.data);
						}else{
							inputSelect.dropCache();
							_this.cache.inputCity = inputSelect({
								id : "cityName",
								key : {
									id : "dictCode",
									name : "dictName",
									data : "data",
								},
								data : data.data,
//								simpleData : true,
								callback : function(ids) {
									var id = ids[0];
									_this.entity.city = id;
									_this.$scope.addSpotForm.city.$setViewValue(id);
									_this.init_zone(id);
								}
							});
						}
					}
				});
			};
			addSupport.fn.init_zone = function(pid) {
				var _this = this;
				$.ajax({
					url : getServer() + "/flea/hclv/getZone.htmls",
					data:{'pcode': pid},
					success : function(data) {
                        if (data.status != 200) {
                            return;
                        }
						if(_this.cache.inputArea){
							_this.cache.inputArea.refreshPanel(data.data);
						}else{
							inputSelect.dropCache();
							_this.cache.inputArea = inputSelect({
								id : "zoneName",
								key : {
									id : "dictCode",
									name : "dictName",
									data : "data",
								},
								data : data.data,
								simpleData : true,
								// 启用文本搜索
								// initData:[initProvice],
								searchAble : false,
								callback : function(id) {
									_this.entity.area = id[0];
									_this.$scope.addSpotForm.area.$setViewValue(_this.entity.area);
								}
							});
						}
					}
				});
			}
			addSupport.fn.baseInit = function() {
				var _this = this;
				//加载code
				$.ajax({
					url : getServer()
							+ "/flea/hclv/getUUID.htmls",
					method : 'get',
					success : function(data) {
                        if (data.status != 200) {
                            return;
                        }
						_this.entity.code = data.data;
						_this.digest();
					}
				});
				//星级
				inputSelect.dropCache();
				inputSelect({
					id : "levelName",
					key : {
						id : "dictCode",
						name : "dictName",
						data : "data",
					},
					data : _this.$scope.cache.typeArray,
					simpleData : true,
					// 启用文本搜索
					// initData:[initProvice],
					searchAble : false,
					callback : function(id) {
						_this.entity.level = id[0];
						_this.$scope.addSpotForm.level.$setViewValue(_this.entity.level);
					}
				});
				// 初始化省下拉框
				inputSelect.dropCache();
				inputSelect({
					id : "provinceName",
					key : {
						id : "dictCode",
						name : "dictName",
						data : "data",
					},
					data : _this.$scope.cache.province,
					simpleData : true,
					callback : function(id) {
						var id = id[0];
						_this.entity.province = id;
						_this.$scope.addSpotForm.province.$setViewValue(id);
						_this.init_city(id);
					}
				});
				MFU.init({
	                placeAt:"multiFileUpload",
	                allowPhyDelete:true,
	                allowDelete:true,
	                allowMC:true,
	                onUploadSuccess:function(file){
//	                	_this.entity.images.push({'key':file.name, 'resouceId':file.swordFileId,'path':file.swordFilePath});
	                	$.each(_this.bind.images, function(index, row){
	                    	if(row.key == file.name){
	                    		row = $.extend(row, {
	                    			resouceId: file.swordFileId,
	                    			path: file.swordFilePath
	                    		});
	                    	}
	                    });
	                	_this.bind.isOk = true;
	                	_this.$scope.addSpotForm.isOk.$setViewValue(true);
	                },
	                onDeleteSavedFile:function(file){
	                	if(file == null)
	                		return;
	                	var i = 0;
	                    $.each(_this.bind.images, function(index, row){
	                    	if(row.resouceId == file.swordFileId){
	                    		i = index;
	                    		return
	                    	}
	                    });
	                    _this.bind.images.splice(i, 1);
	                    if(_this.bind.images.length == 0){
	                    	_this.bind.isOk = '';
	                    	_this.$scope.addSpotForm.isOk.$setViewValue('');
	                    }
	                    _this.digest();
	                },
	                onFilesQueued: function(files){
	                	var webUploader = this.webUploader, queuedFiles = this.queuedFiles;
                        $.each(files, function(index, file) {
                            if(ImageSuffix.indexOf(file.ext.toLowerCase()) < 0){
                                $.each(queuedFiles, function(index, item) {
                                    if(file.id == item.file.id){
                                        item.remove();
                                    }
                                });
                                return true;
                            }
	                		webUploader.makeThumb( file, function( error, src ) {
		                        if ( error ) {
		                        	Util.alert('不能预览');
		                            return;
		                        }
		                        _this.bind.images.push({
		                        	key: file.name,
		                        	src: src
		                        });
		                        _this.digest();
		                    }, 500, 300 );
						});
                        if(queuedFiles.length == 0)
                            Util.alert(ImageSuffixNotSupport);

                        webUploader.upload();
	                }
	            });
			};
			addSupport.fn.save = function() {
				var _this = this, files = new Array();
				$.each(_this.bind.images, function(index, row){
            		files.push({
            			resouceId: row.resouceId,
            			path: row.path
            		});
                });
				$.ajax({
                    url : getServer() + "/flea/hclv/spot/saveOrUpdateSpot.htmls",
                    data : {
                        "model": JSON.stringify(_this.entity), 'files': JSON.stringify(files)
                    },
                    success : function(data) {
                        if (data.status != 200) {
                            return;
                        }
                    	_this.$scope.grid.refresh();
                        Util.alert(data.message);
                        _this.slidebars.close();
                    }
				});
			};
			return addSupportInit;
		});